#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author：TanDabao
# CreateTime：2021/10/27 17:37
from io import StringIO

from chaojiying import Chaojiying_Client
from selenium import webdriver
from PIL import Image
from time import sleep
import requests
import base64

# proxies = {"http": None, "https": None}
# headers = {
#     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'
# }

# driver = webdriver.Chrome(r"C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver = webdriver.Chrome(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.implicitly_wait(10)
driver.get("https://passport.ksyun.com/login.html")
user = ''
passwd = ''

driver.switch_to.frame('ksc_login_iframe1')

# 获取当前cookies
cookies_list = driver.get_cookies()
# print('get_cookies:', cookies_list)
dic1, dic2, dic3 = {}, {}, {}
dic1[cookies_list[0]['name']] = cookies_list[0]['value']
dic2[cookies_list[1]['name']] = cookies_list[1]['value']
dic3[cookies_list[2]['name']] = cookies_list[2]['value']

# 获取cookie中的name和value,转化成requests可以使用的形式
# for cookie in c:
#     cookies[cookie['name']] = cookie['value']

urer_input = driver.find_element_by_xpath('//*[@id="username"]')
pass_input = driver.find_element_by_xpath('//*[@id="password"]')
urer_input.send_keys(user)
pass_input.send_keys(passwd)
driver.find_element_by_xpath('//*[@id="login"]').click()

sleep(2)
src = driver.find_element_by_xpath('/html/body/div[2]/form/div[6]/div[2]/div[1]/img').get_attribute('src')
print(src)
print(type(src))
strs = src.replace('data:image/jpeg;base64,','').encode('utf-8')
imgdata = base64.b64decode(strs)
with open('code.png', 'wb') as f:
    f.write(imgdata)

